package day04.concurrent01;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 1阻塞式队列 BlockingQueue
 */
public class Demo01 {
    public static void main(String[] args) throws Exception {
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(5);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    int i = 0;
                    while(true){
                        Thread.sleep(5000);
                        queue.put(i++);
                        System.out.println(Thread.currentThread()+"写入了数据："+i);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while(true){
                        Thread.sleep(2000);
                        Integer i = queue.take();
                        System.out.println(Thread.currentThread()+"读取了数据："+i);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
